package com.zyc.eduservice.controller;


import com.zyc.commonUtils.R;
import com.zyc.eduservice.entity.EduChapter;
import com.zyc.eduservice.entity.vo.ChapterVo;
import com.zyc.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-02-19
 */
@Api("章节管理")
@RestController
@RequestMapping("/eduservice/edu-chapter")
@CrossOrigin //解决跨域
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value = "根据ID查询所有课程详细信息")
    @GetMapping("{courseId}")
    public R getChapterByCourseId(@ApiParam(name = "courseId", value = "课程ID", required = true) @PathVariable String courseId) {
        List<ChapterVo> chapterList = eduChapterService.getChapterList(courseId);
        return R.ok().data("chapterList",chapterList);
    }

    @ApiOperation(value = "增加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "修改章节")
    @PutMapping("updateChapter")
    public R updateChapter(
            @RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "删除章节")
    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@ApiParam(name = "id", value = "章节id", required = true) @PathVariable String id) {
        //判断章节有没有小节，没有才可以删除
        boolean existVideo = eduChapterService.existVideo(id);
        if (existVideo) {
            return R.error();
        } else {
            eduChapterService.removeById(id);
            return R.ok();
        }
    }

    @ApiOperation(value = "查询指定id章节")
    @GetMapping("getChapter/{id}")
    public R getChapter(@ApiParam(name = "id", value = "章节id", required = true) @PathVariable String id) {
        EduChapter chapter = eduChapterService.getById(id);
        return R.ok().data("chapter",chapter);
    }
}

